﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace ChuyenDongVatLy
{
    public partial class frNemxien : Form
    {
        public frNemxien()
        {
            InitializeComponent();
        }

        public delegate void GetString(string vantoc, string gocnghieng, string docao);
        public GetString GetMyValue;
        public delegate void GetString2(bool createfrom);
        public GetString2 GetMyValue2;

        private void btOpen_Click(object sender, EventArgs e)
        {
            if (txbVantoc.Text == "" || txbGocnghieng.Text == "" || tbxdocao.Text == "")
            {
                MessageBox.Show("Ban phai dien day du thong tin!");
            }
            else
            {
                float v = float.Parse(txbVantoc.Text);
                float fi = float.Parse(txbGocnghieng.Text);
                float y = float.Parse(tbxdocao.Text);
                if (v < 0 || fi < 0 || fi > 0.5 || y < 0 || y > 600)
                {
                    if (v < 0)
                        MessageBox.Show("Vận tốc không phù hợp");

                    if (fi < 0 || fi > 0.5)
                        MessageBox.Show("Góc nghiêng không phù hợp");

                    if (y < 0 || y > 600)
                        MessageBox.Show("Cao độ không phù hợp 0->600");
                }
                else if (GetMyValue != null)
                {
                    GetMyValue(txbVantoc.Text, txbGocnghieng.Text, tbxdocao.Text);
                }


            }
            
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (txbGocnghieng.Text == "" || txbVantoc.Text == "" )
                MessageBox.Show("Cần điền đầy đủ thông số !!!");
            else
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Title = "Save as";
                saveFile.Filter = "*.txt|";
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    FileStream f = new FileStream(saveFile.FileName.ToString() + ".txt", FileMode.Create);
                    StreamWriter srw = new StreamWriter(f);
                    srw.Write(txbVantoc.Text);
                    srw.WriteLine();
                    srw.Write(txbGocnghieng.Text);
                    srw.WriteLine();
                    srw.Close();
                    f.Close();
                }
            }            
        }

        private void btLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFile.RestoreDirectory = true;
            DialogResult result = openFile.ShowDialog();
            if (result == DialogResult.OK)
            {
                string filename = openFile.FileName;
                string[] lines = File.ReadAllLines(filename);
                txbVantoc.Text = lines[0].ToString();
                txbGocnghieng.Text = lines[1].ToString();
            }            
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            if (GetMyValue2 != null)
            {
                GetMyValue2(true);
            }
            this.Close();
        }

        private void txbVantoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && txbVantoc.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void txbGocnghieng_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && txbGocnghieng.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void tbxdocao_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbxdocao.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }
    }
}
